package com.welink.cmp.dao.metadata;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.welink.cmp.dao.common.OperationLogService;
import com.welink.cmp.dao.metadata.dataobject.HostDO;
import com.welink.cmp.domain.metadata.enums.ResourceTypeEnum;
import com.welink.cmp.dto.metadata.host.query.HostListByPageQry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 主机 Dao Service
 * 提供主机的CRUD操作
 *
 * @author yanwei li
 * @create 2024-05-20 21:23
 */
@Service
public class HostDaoService extends ServiceImpl<HostMapper, HostDO> {

    @Autowired
    private OperationLogService logService;

    @Transactional
    public HostDO saveHost(HostDO hostDO){
        final boolean save = save(hostDO);
        logService.saveOperationLog(ResourceTypeEnum.HOST.getType(), hostDO.getId(),"新增","admin","操作结果：" +save);
        return hostDO;
    }

    /**
     * 根据 设备名称 查询主机信息
     *
     * @param names 主机ID
     * @return 主机数据对象
     */
    public List<HostDO> getListByName(List<String> names){
        LambdaQueryWrapper<HostDO> wrapper = Wrappers.lambdaQuery(HostDO.class);
        wrapper.in(HostDO::getName,names);
        return this.list(wrapper);
    }

    /**
     * 分页查询接口
     *
     * @param pageQry 分页查询
     * @return 主机列表
     */
    public Page<HostDO> pageQry(HostListByPageQry pageQry) {
        Page<HostDO> page = new Page<>(pageQry.getPageIndex(), pageQry.getPageSize());
        LambdaQueryWrapper<HostDO> wrapper = Wrappers.lambdaQuery(HostDO.class);
        if (pageQry.getName() != null) {
            wrapper.eq(HostDO::getName, pageQry.getName());
        }
        if (pageQry.getId() != null){
            wrapper.eq(HostDO::getId,pageQry.getId());
        }
        if (pageQry.getStrCreateTime() != null) {
            wrapper.ge(HostDO::getCreateTime, pageQry.getStrCreateTime());
        }
        if (pageQry.getEndCreateTime() != null) {
            wrapper.le(HostDO::getCreateTime, pageQry.getEndCreateTime());
        }
        //
        wrapper.select(i-> !i.getColumn().endsWith("_detail"));
        return this.page(page, wrapper);
    }
}

